package com.smartwebx.ha.web.util.json.module;

import java.io.IOException;
import java.util.Date;
import java.util.Objects;

import org.apache.commons.lang3.time.DateFormatUtils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * 格式化时间 年月日
 * @author Administrator
 *
 */
public class DateSerializer extends JsonSerializer<Date>{
	private Object formate = "yyyy-MM-dd";
	
	public DateSerializer(){
		
	}
	 
	public DateSerializer(Object formate){
		this.formate = formate;
	}
	
	@Override
	public void serialize(Date value, JsonGenerator jgen,
			SerializerProvider provider) throws IOException,
			JsonProcessingException {
		String date;
		if (value == null) {
			date = "";
		} else {
			date = DateFormatUtils.format(value, Objects.toString(formate));
		}
		jgen.writeString(date);
	}
}